<?php	namespace Hdphp\Libs;
class SmartyView
{
	private static $smarty=null;
	public function __construct()
	{
		if(!is_null(self::$smarty)) return;
		//实例化smarty
		$smarty = new \Smarty();
		//模板目录【App/Home/View/Index】【App/Admin/View/Arc】
		$smarty->template_dir = "App/".MODULE."/View/" . CONTROLLER;
		//编译目录【Storage/Home/compile/Index】
		$path = "Storage/".MODULE."/compile/" . CONTROLLER;
		is_dir($path) || mkdir($path,0777,true);
		$smarty->compile_dir = $path;
		//缓存目录【Storage/Home/cache/Index】
		$path = "Storage/".MODULE."/cache/" . CONTROLLER;
		is_dir($path) || mkdir($path,0777,true);
		$smarty->cache_dir = $path;
		//是否开启缓存
		$smarty->caching = C("CACHING");
		$smarty->cache_lifetime = C("CACHE_LIFETIME");
		//左右定界符
		$smarty->left_delimiter = C("LEFT_DELIMITER");
		//右定界符
		$smarty->right_delimiter = C("RIGHT_DELIMITER");
		//把$smarty对象交给定义好的smarty属性
		self::$smarty = $smarty;
		
	}
	//显示模板
	protected function display($tpl=null)
	{
		//如果没有传参数（没有说明加载的是哪一个模板）,那么默认加载与方法同名的html模板
		//ECHO ACTION;  index
		$tpl = is_null($tpl) ?  ACTION . ".html": $tpl;
		//调用smarty的display
		self::$smarty->display($tpl);
	}
	//分配变量
	protected function assign($var,$value)
	{
		self::$smarty->assign($var,$value);
	}
}
